<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加权限按钮 -->
      <el-button class="btn-add" size="mini" type="primary" @click="showDialog = true">添加权限</el-button>
      <el-table
        :data="list"
        default-expand-all
        row-key="id"
      >
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type = 1" size="mini" type="text"> 添加 </el-button>
            <el-button size="mini" type="text"> 编辑 </el-button>
            <el-button size="mini" type="text"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      width="500px"
      title="新增权限点"
      :visible.sync="showDialog"
      @close="btnCancel"
    >
      <el-form ref="pressionForm" :model="pressionForm" :rules="rules">
        <el-form-item prop="name" label="权限名称" label-width="120px">
          <el-input v-model="pressionForm.name" size="mini" />
        </el-form-item>
        <el-form-item prop="code" label="权限标识" label-width="120px">
          <el-input v-model="pressionForm.code" size="mini" />
        </el-form-item>
        <el-form-item prop="description" label="权限描述" label-width="120px">
          <el-input v-model="pressionForm.description" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="btnOK">确定</el-button>
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPremissionList, addPremission } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      // list是权限数据
      list: [],
      // showDialog控制添加权限弹层的显示隐藏
      showDialog: false, // 控制弹层显示隐藏
      pressionForm: {
        name: '',
        code: '',
        description: ''
      },
      rules: {
        name: [{
          required: true,
          message: '请输入添加权限的名称',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入标识权限',
          trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this.getPremissionList()
  },
  methods: {
    async getPremissionList() {
      this.list = transListToTreeData(await getPremissionList(), 0)
    },
    btnOK() {
      this.$refs.pressionForm.validate(async isOK => {
        if (isOK) {
          await addPremission(this.pressionForm)
          this.$message.success('添加权限成功')
          this.getPremissionList()
          this.btnCancel()
        }
      })
    },
    // 取消按钮方法
    btnCancel() {
      this.$refs.pressionForm.resetFields()
      this.showDialog = false
    }
  //   async addPremission() {
  //     await addPremission()
  //   }
  }
}
</script>
<style>
.btn-add{
  margin: 10px;
}
</style>
